import component from "element-plus/es/components/tree-select/src/tree-select-option.mjs";
import { createRouter,createWebHistory } from "vue-router";
import{get}from "@/api/jwtApi"
//创建一个路由数组
const routes=[{
    name:"main",
    path:"/main",
    component:()=>import("@/components/main.vue"),
    children: [{
        name: "ear",
        path: "/main/ear",
        component: () => import("@/components/ear.vue")
    }, {
        name: "admin",
        path: "/main/admin",
        component: () => import("@/components/admin.vue")
    },{
        name:"fenlei",
        path:"/main/fenlei",
        component: () => import("@/components/fenlei.vue")
    },{
        name:"shangpin",
        path:"/main/shangpin",
        component: () => import("@/components/shangpin.vue")
    },{
        name:"dingdan",
        path:"/main/dingdan",
        component: () => import("@/components/dingdan.vue")
    },{
        name:"user",
        path:"/main/user",
        component: () => import("@/components/user.vue")
    },{
        name:"show",
        path:"/main/show",
        component:()=>import("@/components/show.vue")
    }]
},{
   name:"login",
   path:"/login",
   component:()=>import("@/components/login.vue") 
}
]
//创建一个路由转发器
const router=createRouter({
    routes,
    history:createWebHistory()
});
//路由守卫
router.beforeEach((to,from,next)=>{
    let jwt=get();
    if(jwt){
        if(to.name==="login"){
            next({name:"ear"})
        }else{
            next();
        }
    }else{
        if(to.name!=="login"){
                next({name:"login"})
        }else{
            next();
        }
    }
})

export default router;